//
//  KeyChainController.m
//  ABlip
//
//  Created by dawid on 08-12-28.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "KeyChainController.h"
#import "AGKeychain.h"
#import "NSData-Base64Extensions.h"

#define KeyChainItemName @"haslo do konta"
#define KeyChainItemKind @"ABlip"

@implementation KeyChainController
-(id)init
{
	if (![super initWithWindowNibName:@"LoginPanel"])
		return nil;
	
	return self;
}
-(IBAction)trySaveToKeyChain:(id)sender
{
#ifdef __DEBUG__
 	NSLog(@"%@", [loginField stringValue])
 #endif;
	NSString *login = [loginField stringValue];
	NSString *passwd = [passwordField stringValue];
	
	if ([login isEqualToString:@""] || [passwd isEqualToString:@""] ){
#ifdef __DEBUG__
 		NSLog(@"Login i/lub haslo nie moga byc puste")
 #endif;

		
	}else {
		[KeyChainController modifyItemFromKeychain:login password:passwd];
		[NSApp stopModal];
	}
	
}
+ (void)modifyItemFromKeychain:(NSString*)login password:(NSString*)passwd
{
	if ([login isEqualToString:@""] || [passwd isEqualToString:@""]) {
		[self displayAlertWithMessageText:@"Don't leave username or password blank"];
		return;
	}
	
	BOOL doesItemExisit = [AGKeychain checkForExistanceOfKeychainItem:KeyChainItemName
														 withItemKind:KeyChainItemKind 
														  forUsername:login ];							
	NSString *passwdToEncode  = [NSString stringWithFormat:@"%@:%@", login, passwd];
	NSData *passwdData = [passwdToEncode dataUsingEncoding:NSUTF8StringEncoding];
	passwdToEncode = [passwdData encodeBase64];

	if (doesItemExisit) {
		BOOL result = [AGKeychain modifyKeychainItem:KeyChainItemName
										withItemKind:KeyChainItemKind
										 forUsername:login
									 withNewPassword:[passwdData encodeBase64]];
		if (!result){
			[self displayAlertWithMessageText:@"Nieudana próba modyfikacji danych w Pęku kluczy"];
		}
	}
	else {
		BOOL result = [AGKeychain addKeychainItem:KeyChainItemName
									 withItemKind:KeyChainItemKind
									  forUsername:login
									 withPassword:[passwdData encodeBase64]];
		if (!result) {
			[self displayAlertWithMessageText:@"Nieudana próba dodania danych do Pęku kluczy"];
		}
	}
}

- (void)getPasswordFromKeychainForLogin:(NSString*)login
{
	if ([login isEqualToString:@""]) {
		[self displayAlertWithMessageText:@"Pole login jest puste"];
		return;
	}
	
	BOOL doesItemExisit = [AGKeychain checkForExistanceOfKeychainItem:KeyChainItemName
														 withItemKind:KeyChainItemKind
														forUsername:login];
	if (doesItemExisit) {
		NSString *password = [AGKeychain getPasswordFromKeychainItem:KeyChainItemName 
														withItemKind:KeyChainItemKind
														 forUsername:login];
		[self displayAlertWithMessageText:password];
	}
	else {
		[self displayAlertWithMessageText:@"The item does not exisit"];
	}
}
-(void)displayAlertWithMessageText:(NSString *)messageText
{
	NSAlert *alert = [[[NSAlert alloc] init] autorelease];
	[[self window] setAlphaValue:1.0];
	[alert addButtonWithTitle:@"Ok"];
	[alert setMessageText:messageText];
	[alert setAlertStyle:NSWarningAlertStyle];
	[alert beginSheetModalForWindow:[self window] modalDelegate:self didEndSelector:nil contextInfo:nil]; 
}
@end
